package com.dduggs.note.compare;

import java.util.*;
import java.util.stream.Collectors;

/**
 * List集合按对象的某个字段去重测试类
 * Created by CJS on 2017/5/26.
 */
public class CompareTest {


    public static void main(String[] args) {

        List<CompanyMsgParam> companyList = new ArrayList<>();
        CompanyMsgParam c1 = new CompanyMsgParam("2", null, "A公司", "0");
        CompanyMsgParam c2 = new CompanyMsgParam("1", "001", "B公司", "1");
        CompanyMsgParam c3 = new CompanyMsgParam("2", "002", "C公司", "2");
        CompanyMsgParam c4 = new CompanyMsgParam("1", "003", "D公司", "3");
        companyList.add(c1);
        companyList.add(c2);
        companyList.add(c3);
        companyList.add(c4);

        /*        Set<CompanyMsgParam> set = new TreeSet<CompanyMsgParam>(new Comparator<CompanyMsgParam>() {
            @Override
            public int compare(CompanyMsgParam com1, CompanyMsgParam com2) {
                return com1.getCompanyCode().compareTo(com2.getCompanyCode());
            }
        });*/
        //lambda
//        Set<CompanyMsgParam> set = new TreeSet<CompanyMsgParam>((com1, com2) -> com1.getCompanyCode().compareTo(com2.getCompanyCode()));
        companyList = companyList.stream().filter(companyMsgParam -> companyMsgParam.getCompanyCode() != null&&!companyMsgParam.getCompanyLevel().equals("3")).collect(Collectors.toList());

//        companyList = companyList.stream().filter(companyMsgParam -> !companyMsgParam.getCompanyLevel().equals("3")).collect(Collectors.toList());

        Set<CompanyMsgParam> set = new TreeSet<CompanyMsgParam>(Comparator.comparing(CompanyMsgParam::getCompanyCode));
        set.addAll(companyList);

        companyList = new ArrayList<CompanyMsgParam>(set);

        for (CompanyMsgParam param : companyList) {
            System.out.println(param.getCompanyCode());
        }
    }
}
